<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter="dataFormSubmit()" label-width="80px">
    <el-form-item label="主键id" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="主键id" disabled></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="营销类别: NEW-新装; SAVE-保存; OTHER-其他 " prop="businessType">
        <el-input v-model="dataForm.businessType" placeholder="营销类别: NEW-新装; SAVE-保存; OTHER-其他 "></el-input>
    </el-form-item>
    <el-form-item label="系统揽收人" prop="receiver">
        <el-input v-model="dataForm.receiver" placeholder="系统揽收人"></el-input>
    </el-form-item>
    <el-form-item label="办理日期" prop="signDate">
        <el-input v-model="dataForm.signDate" placeholder="办理日期"></el-input>
    </el-form-item>
    <el-form-item label="原套餐" prop="oldSet">
        <el-input v-model="dataForm.oldSet" placeholder="原套餐"></el-input>
    </el-form-item>
    <el-form-item label="办理套餐" prop="signSet">
        <el-input v-model="dataForm.signSet" placeholder="办理套餐"></el-input>
    </el-form-item>
    <el-form-item label="系统优惠" prop="systemDiscount">
        <el-input v-model="dataForm.systemDiscount" placeholder="系统优惠"></el-input>
    </el-form-item>
    <el-form-item label="调账金额" prop="adjustAmount">
        <el-input v-model="dataForm.adjustAmount" placeholder="调账金额"></el-input>
    </el-form-item>
    <el-form-item label="甩单编号" prop="orderCode">
        <el-input v-model="dataForm.orderCode" placeholder="甩单编号"></el-input>
    </el-form-item>
    <el-form-item label="营销积分" prop="marketPoint">
        <el-input v-model="dataForm.marketPoint" placeholder="营销积分"></el-input>
    </el-form-item>
    <el-form-item label="主卡号码" prop="masterCard">
        <el-input v-model="dataForm.masterCard" placeholder="主卡号码"></el-input>
    </el-form-item>
    <el-form-item label="副卡1" prop="assistantCard1">
        <el-input v-model="dataForm.assistantCard1" placeholder="副卡1"></el-input>
    </el-form-item>
    <el-form-item label="副卡2" prop="assistantCard2">
        <el-input v-model="dataForm.assistantCard2" placeholder="副卡2"></el-input>
    </el-form-item>
    <el-form-item label="副卡3" prop="assistantCard3">
        <el-input v-model="dataForm.assistantCard3" placeholder="副卡3"></el-input>
    </el-form-item>
    <el-form-item label="副卡4" prop="assistantCard4">
        <el-input v-model="dataForm.assistantCard4" placeholder="副卡4"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/setrecordmarket'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          userId: '',
          businessType: '',
          receiver: '',
          signDate: '',
          oldSet: '',
          signSet: '',
          systemDiscount: '',
          adjustAmount: '',
          orderCode: '',
          marketPoint: '',
          masterCard: '',
          assistantCard1: '',
          assistantCard2: '',
          assistantCard3: '',
          assistantCard4: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],

          businessType: [
            { required: true, message: '营销类别: NEW-新装; SAVE-保存; OTHER-其他 不能为空', trigger: 'blur' }
          ],

          receiver: [
            { required: true, message: '系统揽收人不能为空', trigger: 'blur' }
          ],

          signDate: [
            { required: true, message: '办理日期不能为空', trigger: 'blur' }
          ],

          oldSet: [
            { required: true, message: '原套餐不能为空', trigger: 'blur' }
          ],

          signSet: [
            { required: true, message: '办理套餐不能为空', trigger: 'blur' }
          ],

          systemDiscount: [
            { required: true, message: '系统优惠不能为空', trigger: 'blur' }
          ],

          adjustAmount: [
            { required: true, message: '调账金额不能为空', trigger: 'blur' }
          ],

          orderCode: [
            { required: true, message: '甩单编号不能为空', trigger: 'blur' }
          ],

          marketPoint: [
            { required: true, message: '营销积分不能为空', trigger: 'blur' }
          ],

          masterCard: [
            { required: true, message: '主卡号码不能为空', trigger: 'blur' }
          ],

          assistantCard1: [
            { required: true, message: '副卡1不能为空', trigger: 'blur' }
          ],

          assistantCard2: [
            { required: true, message: '副卡2不能为空', trigger: 'blur' }
          ],

          assistantCard3: [
            { required: true, message: '副卡3不能为空', trigger: 'blur' }
          ],

          assistantCard4: [
            { required: true, message: '副卡4不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
